package com.tulele.vtool.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.tulele.vtool.mapper.ImpressionInfoMapper;
import com.tulele.vtool.model.PlatformSysInfo;
import com.tulele.vtool.model.UserInfo;
import com.tulele.vtool.model.logEntity.ButtonEventInfo;
import com.tulele.vtool.service.IESService;
import com.tulele.vtool.service.IImpressionInfoService;
import com.tulele.vtool.service.IUserInfoService;
import com.tulele.vtool.service.SysGzhService;
import com.tulele.vtool.utils.DateUtil;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class IESServiceImpl implements IESService {

    @Autowired
    private RestHighLevelClient client;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private SysGzhService sysGzhService;

    @Override
    public void putButtonEventIndex(Integer userid,String gzhcode,String eventcode,String eventname) throws IOException {
        IndexRequest indexRequest = new IndexRequest("button_event_index");
        ButtonEventInfo be = new ButtonEventInfo();
        be.setEventcode(eventcode);
        be.setEventname(eventname);
        be.setUserid(userid);
        be.setGzhcode(gzhcode);
        be.setFollowstatus(0);//未跟进

        UserInfo user= userInfoService.getUserBaseInfo(userid);
        if(user!=null && user.getNickName()!=null){
            be.setNickname(user.getNickName());
        }

        PlatformSysInfo gzhInfo= sysGzhService.getPlatformSysInfo(gzhcode);
        if(gzhInfo!=null && gzhInfo.getSysGzh()!=null && gzhInfo.getSysGzh().getGzhName()!=null){
            be.setGzhname(gzhInfo.getSysGzh().getGzhName());
        }

        be.setCreatetime(DateUtil.getFormatDate("yyyy-MM-dd HH:mm:ss",new Date()));
        String jsonString = JSONObject.toJSONString(be);
//        必须指定XContentType
        indexRequest.source(jsonString, XContentType.JSON);
        // 执行
        IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
    }

}
